#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

void getCombination(vector<int> &candidates, int index, vector<vector<int> > &ans, vector<int> &cur, int curSum, int target)
{
    if (curSum == target) {
        ans.push_back(cur);
        return;
    }

    if (curSum > target) return;

    if (index == candidates.size()) return;

    for (int i = index; i < candidates.size(); i++) {
        if (i > index && candidates[i-1] == candidates[i]) continue;
        cur.push_back(candidates[i]);
        getCombination(candidates, i+1, ans, cur, curSum + candidates[i], target);
        cur.pop_back();

    }
}

class Solution {
    public:
        vector<vector<int> > combinationSum2(vector<int> &candidates, int target) {
            vector<vector<int> > ans;
            sort(candidates.begin(), candidates.end());
            vector<int> cur;
            int curSum = 0;
            getCombination(candidates, 0, ans, cur, curSum, target);
            return ans;
        }
};

//test in 1337 online judge and pass all the test.
int main(int argc, char **argv)
{
    Solution mySolution;

    cout << "-----------------Test 1--------------------" << endl;
    int input[] = {3,1,3,5,1,1};
    vector<int> a;
    for (int i = 0; i < sizeof(input)/sizeof(int); i++)
        a.push_back(input[i]);
    vector<vector<int> > ans = mySolution.combinationSum2(a, 8);
    for (int i = 0; i < ans.size(); i++) {
        for (int j = 0; j < ans[i].size(); j++)
            cout << ans[i][j] << " ";
        cout << endl;
    }

    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
